// pages/Unlock/Unlock.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    circleList: [
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      },
      {
        is_checked: false,
        content: "+"
      }
    ],
    num: "",
    startTips: "请记住下方红色的点出现的顺序",
    flag: "-1",
    circleTips: [
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      },
      {
        flag: "text"
      }
    ],
    challengeNum: "1",//挑战次数
    point: "",//难易度显示几个点
    spd: "",//速度
    btnProhibit:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //简单：3*3矩阵    中速5个点 dIdx=1
    //正常：3*3矩阵    中速7个点 dIdx=2
    //困难：3*3矩阵    中速9个点 dIdx=3
    //变态：3*3矩阵    快速9个点 dIdx=4
    //中速1000ms 快速800ms
    var point, spd, dIdx;
    dIdx = options.dIdx;//难易度
    if (dIdx == 1) {
      point = 5;
      spd = 1000;
    } else if (dIdx == 2) {
      point = 7;
      spd = 1000;
    } else if (dIdx == 3) {
      point = 9;
      spd = 1000;
    } else {
      point = 9;
      spd = 800;
    }
    this.setData({
      point: point,
      spd: spd
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    setTimeout(function () {
      that.init();
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.tryAgain();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  lockTap(e) {
    var that = this;
    var btnProhibit = that.data.btnProhibit;
    if (!btnProhibit){
      return;
    }
    var point = that.data.point;
    var flag = that.data.flag;
    that.setData({ flag: ++flag });
    var idx = e.currentTarget.dataset.idx;//点击元素的下标
    var generateData = that.data.tempArray;//随机生成的数据数组
    var circleTips = that.data.circleTips;//底部错误提示
    if (generateData[flag] == idx + 1) {
      circleTips[flag]['flag'] = "right";
      if (point == flag+1){
        wx.navigateTo({
          url: '../../pages/games/games',
        })
      }
    } else {
      circleTips[flag]['flag'] = "wrong";
      wx.showToast({
        title: '游戏结束',
        icon: 'loading',
        mask: true
      })
    }
    var data = that.data.circleList;
    for (var i = 0; i < data.length; i++) {
      data[i].is_checked = false;
    }
    data[idx].is_checked = true;
    that.setData({
      circleList: data,
      num: idx + 1,
      circleTips: circleTips
    })
  },
  init() {
    var that = this;
    var tempArray = new Array();
    var spd = that.data.spd,
        point = that.data.point;
    (function () {
      var i = 0;
      var end = setInterval(function () {
        if (i >= point) {
          setTimeout(function () {
            that.setData({ startTips: "请按照刚刚高亮的顺序依次点击对应的点", btnProhibit:true })
          }, spd)
          clearInterval(end);
        } else {
          that.setData({ startTips: "请记住下方红色的点出现的顺序" })
          var num = Math.floor(Math.random() * 9 + 1);
          tempArray.push(num)
          that.setData({ num: num })
          setTimeout(function () {
            that.setData({ num: "", tempArray: tempArray, btnProhibit:false})
          }, spd / 2)
          i++;
        }
      }, spd);
    })()
  },
  tryAgain() {
    var that = this;
    that.init();
    var challengeNum = that.data.challengeNum;
    var circleTips = that.data.circleTips;
    var circleList = that.data.circleList;
    for (var i = 0; i < circleTips.length; i++) {
      circleTips[i]['flag'] = "text"
      circleList[i]['is_checked'] = false;
    }
    that.setData({
      circleTips: circleTips,
      num: "",
      circleList: circleList,
      flag: "-1",
      challengeNum: ++challengeNum
    })
  }
})